//
//  PullingRefreshView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/13.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "PullingRefreshView.h"

static CGFloat PullingRefreshViewCircleWidth = 30.0f;

@interface PullingRefreshView ()

@property (nonatomic, strong) CAShapeLayer *backgroundCircleLayer;

/**
 *animateCircleLayer
 */
@property (nonatomic, strong) CAShapeLayer *animateCircleLayer;

@end

@implementation PullingRefreshView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubLayers];
    }
    return self;
}
- (void)setupSubLayers {
    self.backgroundCircleLayer = [CAShapeLayer layer];
    self.backgroundCircleLayer.fillColor = [UIColor clearColor].CGColor;
    self.backgroundCircleLayer.lineWidth = 3;
    self.backgroundCircleLayer.strokeColor = [UIColor separatorColor].CGColor;
    [self.layer addSublayer:self.backgroundCircleLayer];
    
    self.animateCircleLayer = [CAShapeLayer layer];
    self.animateCircleLayer.fillColor = [UIColor clearColor].CGColor;
    self.animateCircleLayer.lineWidth = 3;
    self.animateCircleLayer.lineCap = kCALineCapRound;
    self.animateCircleLayer.strokeColor = [UIColor darkSubMainColor].CGColor;
    [self.layer addSublayer:self.animateCircleLayer];
}

- (UIBezierPath *)circleBezierPathWithAngle:(CGFloat)endAngle {
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    CGFloat radius = PullingRefreshViewCircleWidth/2;
    return [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:-M_PI/2 endAngle:endAngle clockwise:true];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundCircleLayer.frame = self.bounds;
    self.animateCircleLayer.frame = self.bounds;
    //
    [self setBackgroundCircleLayerPath];
}
//
- (void)setBackgroundCircleLayerPath {
    self.backgroundCircleLayer.path = [self circleBezierPathWithAngle:M_PI*1.5].CGPath;
}

- (void)updatePullingPercent:(CGFloat)percent {
    if (!_isRefreshing) {
        self.alpha = percent;
        [self updateAnimatelayerCirclePercent:percent];
    }
}

- (void)updateAnimatelayerCirclePercent:(CGFloat)percent {
    percent = MAX(0, MIN(1, percent));
    //
    CGFloat angle = M_PI*2*percent-M_PI/2;
    self.animateCircleLayer.path = [self circleBezierPathWithAngle:angle].CGPath;
}
#pragma mark -
- (void)beginRefreshing {
    self.alpha = 1;
    _isRefreshing = true;
    [self beginAnimate];
}
- (void)endRefreshing {
    self.alpha = 0;
    _isRefreshing = false;
    [self endAnimate];
}

- (void)beginAnimate {
    [self endAnimate];
    //
    [self updateAnimatelayerCirclePercent:0.1];
    //
    CABasicAnimation *transform = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    transform.fromValue = @(0);
    transform.toValue = @(M_PI*4);
    transform.duration = 2;
    transform.repeatCount = HUGE_VALF;
    transform.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transform.removedOnCompletion = false;
    [self.animateCircleLayer addAnimation:transform forKey:@""];
    
    //
    CABasicAnimation *transform2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    transform2.fromValue = @(0);
    transform2.toValue = @(M_PI*2);
    transform2.duration = transform.duration*5;
    transform2.repeatCount = HUGE_VALF;
    transform2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transform2.removedOnCompletion = false;
    [self.backgroundCircleLayer addAnimation:transform2 forKey:@""];
    
}

- (void)endAnimate {
    [self.backgroundCircleLayer removeAllAnimations];
    [self.animateCircleLayer removeAllAnimations];
    //
    [self updateAnimatelayerCirclePercent:0];
}

@end
